package com.yxkj.syh.app.huarong.activities.creat.edit;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.OrderInfo;
import com.yxkj.syh.app.huarong.bean.ScanCarIdResponse;
import com.yxkj.syh.app.huarong.bean.UploadImageResponse;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;
import com.yxkj.syh.app.huarong.data_center.model.UploadModel;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 创建者：SYH
 * 时间：2020/5/28 17:38
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class EditOrderVM extends BaseViewModel<ActivityEvent> {
    public OrderInfo orderInfo;

    public EditOrderVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofLineType = new ObservableField<>();
    public ObservableField<String> ofFromAddress = new ObservableField<>();
    public ObservableField<String> ofToAddress = new ObservableField<>();
    public ObservableField<String> ofCarHeadNo = new ObservableField<>();
    public ObservableField<String> ofCarTrailerNo = new ObservableField<>();
    public ObservableField<String> ofRemark = new ObservableField<>();
    public ObservableField<Boolean> ofIsEmptyCar = new ObservableField<>(Boolean.FALSE);

    public MutableLiveData<String> mldImageUrl = new MutableLiveData<>();
    public MutableLiveData<Boolean> mldFache = new MutableLiveData<>();

    public void initOrderInfo(OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
        ofLineType.set(orderInfo.getLineType() == 0 ? "直达" : "回头车");
        ofFromAddress.set(orderInfo.getStartArea() + orderInfo.getStartAddress());
        ofToAddress.set(orderInfo.getEndArea() + orderInfo.getEndAddress());
        ofCarHeadNo.set(orderInfo.getCarNo());
        ofCarTrailerNo.set(orderInfo.getSemitrailerNo());
        ofIsEmptyCar.set(orderInfo.getIsEmpty() == 0 ? false : true);
    }

    public void fache(List<String> shdfUrls) {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("orderId", orderInfo.getId());
        if (!ofIsEmptyCar.get()) {
            JSONArray credentials = new JSONArray();
            for (String s:shdfUrls){
                credentials.put(s);
            }
            pMap.put("credentials", credentials.toString());
            pMap.put("isEmpty",0);
        }else {
            pMap.put("isEmpty",1);
        }
        pMap.put("remark", ofRemark.get());

        showLoading("orderFache");
        OrderModel.getOrderModel().orderFache(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("orderFache");
                Tooast.success(response.getMessage());
                mldFache.setValue(true);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("orderFache");
                mldFache.setValue(false);
                Tooast.warning(msg);
            }
        });
    }

    /**
     * 上传图片
     *
     * @param file
     */
    public void uploadImage(File file) {
        showLoading("getUploadModel");
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
        UploadModel.getUploadModel().uploadImage(filePart, mLifecycleProvider.bindToLifecycle(), new ApiObserver<UploadImageResponse>() {
            @Override
            public void onSuccess(UploadImageResponse response) {
                hideLoading("getUploadModel");
                mldImageUrl.setValue(response.getData());
                Tooast.success("上传成功");
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getUploadModel");
                Tooast.warning(msg);
            }
        });
    }


}
